package ${packageName}.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import tjs.wf.util.ExcelData;
import tjs.wf.util.ExcelUtil;

import ${packageName}.dao.${classType}Dao;
import ${packageName}.bean.${classType};

@Service
public class ${classType}Service {

    @Autowired
	private ${classType}Dao ${className}Dao;
	
	public List<${classType}> list(${classType} ${className}) {
	    QueryWrapper<${classType}> wrapper = new QueryWrapper<>();
        setQueryOption(${className},wrapper);
		List<${classType}> list = ${className}Dao.selectList(wrapper);
		return list;
	}

	public IPage<${classType}> pageList(${classType} ${className}){
    	QueryWrapper<${classType}> wrapper = new QueryWrapper<>();
    	setQueryOption(${className},wrapper);
    	return ${className}Dao.selectPage(${className}.page(),wrapper);
    }

    public void setQueryOption(${classType} ${className}, QueryWrapper<${classType}> wrapper){

    }

    public void report(${classType} ${className}, HttpServletResponse response) throws Exception {
    	QueryWrapper<${classType}> wrapper = new QueryWrapper<>();
    	IPage<${classType}> list = ${className}Dao.selectPage(${className}.page(),wrapper);

    	ExcelData data = new ExcelData();
    	data.setName("test");
    	List<String> titles = new ArrayList();
    	titles.add("ID");
    	data.setTitles(titles);
    	List<${classType}> ${className}List = list.getRecords();
    	Collections.reverse(${className}List);
    	List<List<Object>> rows = new ArrayList();
    	for(int i = 0, length = list.getRecords().size();i<length;i++){
    		${classType} obj = list.getRecords().get(i);
    		List<Object> row = new ArrayList();
    		row.add("id");
    		rows.add(row);
    	}
    	data.setRows(rows);
    	ExcelUtil.exportExcel(response,"testExcel",data);
    }
	
	public ${classType} get(${idType} ${id}) {
		return ${className}Dao.selectById(${id});
	}
	
	public ${classType} save(${classType} ${className}) {
		if(${className}.getId() != null){
		    ${className}Dao.updateById(${className});
		}else{
		    ${className}Dao.insert(${className});
		}
		return ${className};
	}
	
	public ${classType} update(${classType} ${className}) {
		${className}Dao.updateById(${className});
		return ${className};
	}
	
	public void delete(${idType} ${id}) {
		${className}Dao.deleteById(${id});
	}
    
}